package com.easy.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easy.common.bean.Product;
import com.easy.common.service.IProductService;
import com.easy.common.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    IProductService productService;
    @PostMapping("/")
    public ResultData insert(@RequestBody Product product) {
        Product data=productService.insert(product);
        System.out.println(data);
        return ResultData.success(data);
    }
    @PutMapping("/")
    public ResultData update(@RequestBody Product product) {
        Product data=productService.update(product);
        return ResultData.success(data);
    }
    @DeleteMapping("/{id}")
    public ResultData delete(@PathVariable Integer id) {
        int result=productService.delete(id);
        if(result>=1){
            return ResultData.success("200","删除成功",null);
        } else {
            return ResultData.fail();
        }
    }
    @GetMapping("/{id}")
    public ResultData getByID(@PathVariable Integer id) {
        Product product=productService.getByID(id);
        return ResultData.success(product);
    }
    @GetMapping("/page")
    public ResultData getListByPage(IPage page, Product product) {
        IPage<Product> data=productService.getListByPage(page,product);
        return ResultData.success(data);
    }
    @GetMapping("/list")
    public ResultData getList(Product product) {
        List<Product> data=productService.getList(product);
        return ResultData.success(data);
    }
}
